Arduino robot car 2 wheels - control by Bluetooth

Click Here to View Step by Step

Arduino robot car 

 

In this lesson, you will learn how to use an Arduino to build a 2WD robotic car. control by Bluetooth 

Required parts

Here is what you will need:

  • 1 x 2WD robotic car kit (2 wheels, 2 motors, and caster wheel)

  • 1 x L298P Shield R3 DC Motor shield

  • 3* 18650 batteries

  • Esp32s development board wifi and Bluetooth 

 

About L298N Motor Driver 

L298N Motor Driver

his L298N Motor Driver Module is a high-power motor driver module for driving DC and Stepper Motors. This module consists of an L298 motor driver IC and a 78M05 5V regulator. L298N Module can control up to 4 DC motors, or 2 DC motors with directional and speed control.

298N Module Pinout Configuration

Pin Name

Description

IN1 & IN2

Motor A input pins. Used to control the spinning direction of Motor A

IN3 & IN4

Motor B input pins. Used to control the spinning direction of Motor B

ENA

Enables PWM signal for Motor A

ENB

Enables PWM signal for Motor B

OUT1 & OUT2

Output pins of Motor A

OUT3 & OUT4

Output pins of Motor B

12V

12V input from DC power source

5V

Supplies power for the switching logic circuitry inside L298N IC

GND

Ground pin

Features & Specifications

  • Driver Model: L298N 2A
  • Driver Chip: Double H Bridge L298N
  • Motor Supply Voltage (Maximum): 46V
  • Motor Supply Current (Maximum): 2A
  • Logic Voltage: 5V
  • Driver Voltage: 5-35V
  • Driver Current:2A
  • Logical Current:0-36mA
  • Maximum Power (W): 25W
  • Current Sense for each motor
  • Heatsink for better performance
  • Power-On LED indicator

About ESP32s - 38pin

esp32 38 pin

 

SMARTROBOTCAR_Bluetooth

#include "BluetoothSerial.h"
#define ENA   14          // Enable/speed motors Right        GPIO14(D5)
#define ENB   12          // Enable/speed motors Left         GPIO12(D6)
#define IN_1  4          // L298N in1 motors Right           GPIO15(D2)
#define IN_2  13          // L298N in2 motors Right           GPIO13(D7)
#define IN_3  2           // L298N in3 motors Left            GPIO2(D4)
#define IN_4  0           // L298N in4 motors Left            GPIO0(D3)
#define front_light  15   
#define red_light  5           //back
char commandm;
            //String to store app command state.
int speedCar = 1000;         // 400 - 1023.
int speed_Coeff = 3; /* Check if Bluetooth configurations are enabled in the SDK */
/* If not, then you have to recompile the SDK */
  Serial.begin(115200);
  /* If no name is given, default 'ESP32' is applied */
  /* If you want to give your own name to the ESP32 Bluetooth device, then */
  /* specify the name as an argument SerialBT.begin("myESP32Bluetooth*/
  SerialBT.begin("Car Domy");// BTName
  SerialBT.begin();
 pinMode(ENA, OUTPUT);
 pinMode(ENB, OUTPUT);  
 pinMode(IN_1, OUTPUT);
 pinMode(IN_2, OUTPUT);
 pinMode(IN_3, OUTPUT);
 pinMode(IN_4, OUTPUT); 
  pinMode(IN_4, OUTPUT); 
   pinMode(red_light, OUTPUT); 
    pinMode(front_light, OUTPUT);
} void loop() {
 
    if (SerialBT.available())
  {
   // Serial.write(SerialBT.read());
    commandm = SerialBT.read();
  }
  if (commandm == 'F')
  {
    Serial.write("walid");
goAhead();
    }
    if (commandm == 'B')
  {
    Serial.write("walid");
  goBack();
    }
      if (commandm == 'L')
  {
    Serial.write("walid");
   goLeft();
    }
         if (commandm == 'R')
  {
    Serial.write("walid");
 goRight();
    }
          if (commandm == 'I')
  {
    Serial.write("walid");
goAheadRight();
    }
              if (commandm == 'I')
  {
    Serial.write("walid");
goAheadRight();
    }
                if (commandm == 'G')
  {
    Serial.write("walid");
goAheadLeft();
    }
                if (commandm == 'J')
  {
    Serial.write("walid");
goBackRight();
    }
                if (commandm == 'H')
  {
    Serial.write("walid");
goBackLeft();
    }
                if (commandm == '0')
  {
    Serial.write("walid");
 speedCar = 400;
    }
                if (commandm == '1')
  {
    Serial.write("walid");
 speedCar = 500;
    }
                if (commandm == '2')
  {
    Serial.write("walid");
 speedCar = 600;
    }
                if (commandm == '3')
  {
    Serial.write("walid");
 speedCar = 650;
    }
                if (commandm == '4')
  {
    Serial.write("walid");
 speedCar = 700;
    }
                if (commandm == '5')
  {
    Serial.write("walid");
 speedCar = 750;
    }
                if (commandm == '6')
  {
    Serial.write("walid");
 speedCar = 800;
    }
                if (commandm == '7')
  {
    Serial.write("walid");
 speedCar = 850;
    }
                if (commandm == '8')
  {
    Serial.write("walid");
 speedCar = 900;
    }
                if (commandm == '9')
  {
    Serial.write("walid");
 speedCar = 950;
    }
                if (commandm == '10')
  {
    Serial.write("walid");
 speedCar = 1023;
    }
                if (commandm == 'S')
  {
    Serial.write("walid");
stopRobot();
    }
  delay(20);
}


void goAhead(){ 
    digitalWrite(IN_1, LOW);
      digitalWrite(IN_2, HIGH);
      analogWrite(ENA, speedCar);       digitalWrite(IN_3, HIGH);
      digitalWrite(IN_4, LOW);
      analogWrite(ENB, speedCar);
      
  
      
  digitalWrite(red_light, LOW);
 digitalWrite(front_light, HIGH);    
  } void goBack(){  digitalWrite(red_light, HIGH);
digitalWrite(front_light,LOW );           digitalWrite(IN_1, HIGH);
      digitalWrite(IN_2, LOW);
      analogWrite(ENA, speedCar);       digitalWrite(IN_3, LOW);
      digitalWrite(IN_4, HIGH);
      analogWrite(ENB, speedCar); void goRight(){ 
digitalWrite(front_light,LOW );      
digitalWrite(red_light, HIGH);        digitalWrite(IN_1, LOW);
      digitalWrite(IN_2, HIGH);
      analogWrite(ENA, speedCar);       digitalWrite(IN_3, LOW);
      digitalWrite(IN_4, HIGH);
      analogWrite(ENB, speedCar);
  } void goLeft(){
  digitalWrite(front_light,LOW );    
digitalWrite(red_light, HIGH);
       digitalWrite(IN_1, HIGH);
      digitalWrite(IN_2, LOW);
      analogWrite(ENA, speedCar);
      digitalWrite(IN_3, HIGH);
      digitalWrite(IN_4, LOWnm);
      analogWrite(ENB, speedCar);
  }
void goAheadRight(){
      digitalWrite(front_light,LOW );    
      digitalWrite(IN_1, LOW);
      digitalWrite(IN_2, HIGH);
      analogWrite(ENA, speedCar/speed_Coeff);
 digitalWrite(red_light, HIGH);
      digitalWrite(IN_3, LOW);
      digitalWrite(IN_4, HIGH);
      analogWrite(ENB, speedCar);
   }
void goAheadLeft(){
  digitalWrite(front_light,LOW );    
      digitalWrite(red_light, HIGH);
      digitalWrite(IN_1, LOW);
      digitalWrite(IN_2, HIGH);
      analogWrite(ENA, speedCar);
      digitalWrite(IN_3, LOW);
      digitalWrite(IN_4, HIGH);
      analogWrite(ENB, speedCar/speed_Coeff);
  }
void goBackRight(){ 
      digitalWrite(front_light,LOW );    
      digitalWrite(red_light, HIGH);
      digitalWrite(IN_1, HIGH);
      digitalWrite(IN_2, LOW);
      analogWrite(ENA, speedCar/speed_Coeff);


      digitalWrite(IN_3, HIGH);
      digitalWrite(IN_4, LOW);
      analogWrite(ENB, speedCar);
  }
void goBackLeft(){ 
      digitalWrite(front_light,LOW );    
      digitalWrite(red_light, HIGH);
      digitalWrite(IN_1, HIGH);
      digitalWrite(IN_2, LOW);
      analogWrite(ENA, speedCar);
      digitalWrite(IN_3, HIGH);
      digitalWrite(IN_4, LOW);
      analogWrite(ENB, speedCar/speed_Coeff);
  }
void stopRobot(){  
      digitalWrite(red_light, LOW);
      digitalWrite(IN_1, LOW);
      digitalWrite(IN_2, LOW);
      analogWrite(ENA, speedCar);
      digitalWrite(front_light,LOW );    
      digitalWrite(IN_3, LOW);
      digitalWrite(IN_4, LOW);
      analogWrite(ENB, speedCar);
 }